package com.linewow.xhyy.nihaonew.activity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.adapter.ImessageAdapter;
import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.base.BaseActivity;
import com.linewow.xhyy.nihaonew.base.BaseInfo;
import com.linewow.xhyy.nihaonew.entity.ImessageInfo;
import com.linewow.xhyy.nihaonew.entity.user.Uinfo;
import com.linewow.xhyy.nihaonew.model.AppConstants;
import com.linewow.xhyy.nihaonew.net.NihaoNetWork;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by LXR on 2016/10/17.
 * 消息模块的 信息
 */
public class IMessageActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {


    @Bind(R.id.toolbar_back_title)
    TextView toolbarBackTitle;
    @Bind(R.id.toolbar_back_left)
    ImageView toolbarBackLeft;
    @Bind(R.id.imessage_recycler)
    RecyclerView imessageRecycler;

    private String TAG="IMessageActivity";
    private ImessageAdapter adapter;
    private String type;
    private List<ImessageInfo<Uinfo>>imessageInfos;
    private int page=1;
    private String str;


    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_imessage);
        ButterKnife.bind(this);
        LinearLayoutManager manager=new LinearLayoutManager(IMessageActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        imessageRecycler.setLayoutManager(manager);
        imessageRecycler.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initList() {
        super.initList();
        type=getIntent().getStringExtra("info");
        switch (type){
            case AppConstants.REPLYPARA:
                toolbarBackTitle.setText(getResources().getString(R.string.title_reply));
                break;
            case AppConstants.LIKEPARA:
                toolbarBackTitle.setText(getResources().getString(R.string.title_like));
                break;
            case AppConstants.SYSTEMPARA:
                toolbarBackTitle.setText(getResources().getString(R.string.title_system));
                break;
        }

        String string= NiHaoApp.token;
         str=null;
        try {
            str= URLEncoder.encode(string,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Log.e(TAG,"请求到的 type"+type+"str"+str);
        page=1;
        requestInfo();
    }

    private void requestInfo() {
        if(type==null||str==null){
            return;
        }
        NihaoNetWork.getNiHaoService()
                .getImessageInfo(type,str,page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseInfo<ImessageInfo<Uinfo>>>() {
                    @Override
                    public void onCompleted() {
                        Log.e(TAG,"完成了");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG,"信息"+e.getMessage());
                    }

                    @Override
                    public void onNext(BaseInfo<ImessageInfo<Uinfo>> imessageInfoBaseInfo) {
                        Log.e(TAG,"请求成功"+imessageInfoBaseInfo.data.get(0).type);
                        imessageInfos=imessageInfoBaseInfo.data;
                        if(adapter==null){
                            if(page==0){
                                adapter=new ImessageAdapter(imessageInfos);
                                adapter.isFirstOnly(false);
                                adapter.setOnLoadMoreListener(IMessageActivity.this);
                                imessageRecycler.setAdapter(adapter);
                                if(imessageInfos.size()==0){
                                    showNoData();
                                }else{
                                    dismissNoData();
                                }
                            }
                        }else{
                            adapter.addData(imessageInfos);
                            if(imessageInfos.size()<20){
                                adapter.loadComplete();
                            }
                        }
                    }
                });


    }


    @Override
    public void initData() {
        super.initData();
    }


    @OnClick(R.id.toolbar_back_left)
    public void onClick() {
    }

    @Override
    public void onLoadMoreRequested() {
        if(adapter!=null){
            Log.e(TAG,"上拉加载了");
            page++;
            requestInfo();
        }
    }
}
